﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class ThongKe_frmPhuluc01 : System.Web.UI.Page
{

    BieumauDataContext _db = new BieumauDataContext();
    string _maDonVi = "";
    int _nam = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

         _maDonVi = Request.QueryString["id"].ToString().Trim();
         _nam =Convert.ToInt32( Request.QueryString["nam"].ToString().Trim());
            if (Session["login_admin"] == null)
                {
                    Response.Redirect("../Index.aspx");
                }
            // lblNgayGui.Text = DateTime.Now.ToShortDateString();
           lblNam.Text =(_nam - 1) + "";
            lblDonViBaoCao.Text = ((tblCoQuanNhaNuoc)_db.tblCoQuanNhaNuocs.Single(p => p.MaCoQuan == _maDonVi)).TenCoQuan;
            if (!IsPostBack)
            {
                loadDuLieu();
            }
        

    }

    // load dữ liệu cũ lên page
    void loadDuLieu()
    {
        try
        {
            tblBieuMau bm = _db.tblBieuMaus.Single(p => p.MaDonVi == _maDonVi
                                                       && p.NamBaoCao ==_nam
                                                       && p.TenBieuMau == "PL_01");

            string _dulieu = bm.DuLieu;

            string _noinhan = bm.NoiNhan;
            if (_noinhan.Contains("Cục Thông tin KH&CN Quốc gia"))
                chkDonViNhan_CucThongTin.Checked = true;
            if (_noinhan.Contains("Sở KH&CN tỉnh, tp. trực thuộc TƯ"))
                chkDonViNhan_SoKHCN.Checked = true;
            if (_noinhan.Contains("Bộ/ngành chủ quản"))
                chkDonViNhan_BoNganhChuQuan.Checked = true;

            string[] _arrDuLieuTach = _dulieu.Split('~');
            txtNguoiLapBieu.Text = _arrDuLieuTach[0];
            txtGiamDoc.Text = _arrDuLieuTach[1];
            txtDiaDiemKy.Text = _arrDuLieuTach[2];

            // 10 
            txtTenDonVi.Text = _arrDuLieuTach[3];
            txtTenGiaoDich.Text = _arrDuLieuTach[4];
            txtTenTiengAnh.Text = _arrDuLieuTach[5];

            txtDiaChi.Text = _arrDuLieuTach[6];
            txtTinhTP.Text = _arrDuLieuTach[7];
            txtDienThoai.Text = _arrDuLieuTach[8];
            txtFax.Text = _arrDuLieuTach[9];
            txtEmail.Text = _arrDuLieuTach[10];
            txtWebsite.Text = _arrDuLieuTach[11];

            txtSoQuyetDinhThanhLap.Text = _arrDuLieuTach[12];
            // 10
            txtNgayThanhLap.Text = _arrDuLieuTach[13];
            txtMaSoThue.Text = _arrDuLieuTach[14];
            txtSoChungNhanDangKy.Text = _arrDuLieuTach[15];
            txtNgayChungNhanDangKy.Text = _arrDuLieuTach[16];
            txtNoiCapChungNhanDangKy.Text = _arrDuLieuTach[17];

            chkCapChinhPhu.Checked = _arrDuLieuTach[18].Equals("True") ? true : false;
            chkCapThuTuongChinhPhu.Checked = _arrDuLieuTach[19].Equals("True") ? true : false;
            chkCapBoNganh.Checked = _arrDuLieuTach[20].Equals("True") ? true : false;
            chkCapTinh.Checked = _arrDuLieuTach[21].Equals("True") ? true : false;
            chkCapCoQuanTWDoanThe.Checked =  _arrDuLieuTach[22].Equals("True") ? true : false;
            chkCapCoSo.Checked = _arrDuLieuTach[23].Equals("True") ? true : false;  
            //
            txtCoQuanQuanLiTrucTiep.Text = _arrDuLieuTach[24];
            txtBoNganhChuQuan.Text = _arrDuLieuTach[25];

            //
            chkLoaiHinh_HanhChinh.Checked = _arrDuLieuTach[26].Equals("True") ? true : false;  
            chkLoaiHinh_DonViNghienCuu.Checked =_arrDuLieuTach[27].Equals("True") ? true : false;  
            chkLoaiHinh_Truong.Checked= _arrDuLieuTach[28].Equals("True") ? true : false;  
            chkLoaiHinh_DonViDichVu.Checked= _arrDuLieuTach[29].Equals("True") ? true : false; 
            chkLoaiHinh_DoanhNghiepKHCN.Checked = _arrDuLieuTach[30].Equals("True") ? true : false;
            chkLoaiHinh_DoanhNghiep.Checked = _arrDuLieuTach[31].Equals("True") ? true : false; 
            //
            chkThanhPhan_NhaNuoc.Checked = _arrDuLieuTach[32].Equals("True") ? true : false;
            chkThanhPhan_TapThe.Checked = _arrDuLieuTach[33].Equals("True") ? true : false;
            chkThanhPhan_TuNhan.Checked = _arrDuLieuTach[34].Equals("True") ? true : false;
            chkThanhPhan_VonNuocNgoai.Checked = _arrDuLieuTach[35].Equals("True") ? true : false;

            txtTaiSanCoDinh.Text = _arrDuLieuTach[36];
            txtVonLuuDong.Text = _arrDuLieuTach[37];
            txtMoTa.Text = _arrDuLieuTach[38];
            //
            chkBaoGom.Checked = _arrDuLieuTach[39].Equals("True") ? true : false;
            chkChiBaoGom.Checked = _arrDuLieuTach[40].Equals("True") ? true : false;

            // thuật toán điền dữ liệu lên bảng
            dienDuLieuLenPage(3, 3, "txt", _arrDuLieuTach, 41);

            if (bm.DaNop == true)  // nếu nộp rồi thì khóa
            {
                duyetAllTextBox(3, 3, "txt", "khoa");
                btnNop.Enabled = false;
                lblNgayGui.Text = ((DateTime)bm.NgayNop).ToShortDateString();
                txtNgayKy.Text = ((DateTime)bm.NgayNop).Day.ToString();
                txtThangKy.Text = ((DateTime)bm.NgayNop).Month.ToString();
                txtNamKy.Text = ((DateTime)bm.NgayNop).Year.ToString();
            }
            else // chưa nộp thì set background cho phép sửa
            {
                duyetAllTextBox(3, 3, "txt", "backcolor");
                btnNop.Enabled = true;
            }

        }
        catch (Exception ex)
        {
            duyetAllTextBox(3, 3, "txt", "backcolor");
            btnNop.Enabled = true;
        }
    }

    private void duyetAllTextBox(int _soDong, int _soCot, string _tenGocControl, string lenh)
    {
        txtNgayKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
        txtThangKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
        txtNamKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);

        if (lenh.Equals("khoa")) // neu lenh là khóa thì khóa
        {
            // đầu tiên khóa các Control phụ
            chkDonViNhan_BoNganhChuQuan.Enabled = false;
            chkDonViNhan_CucThongTin.Enabled = false;
            chkDonViNhan_SoKHCN.Enabled = false;
            txtNguoiLapBieu.ReadOnly = true;
            txtGiamDoc.ReadOnly = true;
            txtDiaDiemKy.ReadOnly = true;
            // 10 
            txtTenDonVi.ReadOnly = true;
            txtTenGiaoDich.ReadOnly = true;
            txtTenTiengAnh.ReadOnly = true;
            txtDiaChi.ReadOnly = true;
            txtTinhTP.ReadOnly = true;
            txtDienThoai.ReadOnly = true;
            txtFax.ReadOnly = true;
            txtEmail.ReadOnly = true;
            txtWebsite.ReadOnly = true;
            txtSoQuyetDinhThanhLap.ReadOnly = true;
            txtNgayThanhLap.ReadOnly = true;
            txtMaSoThue.ReadOnly = true;
            txtSoChungNhanDangKy.ReadOnly = true;
            txtNgayChungNhanDangKy.ReadOnly = true;
            txtNoiCapChungNhanDangKy.ReadOnly = true;
            chkCapChinhPhu.Enabled = false;
            chkCapThuTuongChinhPhu.Enabled = false;
            chkCapBoNganh.Enabled = false;
            chkCapTinh.Enabled = false;
            chkCapCoQuanTWDoanThe.Enabled = false;
            chkCapCoSo.Enabled = false;
            txtCoQuanQuanLiTrucTiep.ReadOnly = true;
            txtBoNganhChuQuan.ReadOnly = true;
            chkThanhPhan_NhaNuoc.Enabled = false;
            chkThanhPhan_TapThe.Enabled = false;
            chkThanhPhan_TuNhan.Enabled = false;
            chkThanhPhan_VonNuocNgoai.Enabled = false;
            txtTaiSanCoDinh.ReadOnly = true;
            txtVonLuuDong.ReadOnly = true;
            txtMoTa.ReadOnly = true;
            chkBaoGom.Enabled = false;
            chkChiBaoGom.Enabled = false;
            chkLoaiHinh_DoanhNghiep.Enabled = false;
            chkLoaiHinh_DoanhNghiepKHCN.Enabled = false;
            chkLoaiHinh_DonViDichVu.Enabled = false;
            chkLoaiHinh_DonViNghienCuu.Enabled = false;
            chkLoaiHinh_HanhChinh.Enabled = false;
            chkLoaiHinh_Truong.Enabled = false;

            ////
            txtNguoiLapBieu.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtGiamDoc.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtDiaDiemKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkDonViNhan_BoNganhChuQuan.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkDonViNhan_CucThongTin.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkDonViNhan_SoKHCN.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            //
            txtTenDonVi.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtTenGiaoDich.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtTenTiengAnh.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtDiaChi.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtTinhTP.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtDienThoai.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtFax.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtEmail.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtWebsite.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtSoQuyetDinhThanhLap.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtNgayThanhLap.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtMaSoThue.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtSoChungNhanDangKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtNgayChungNhanDangKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtNoiCapChungNhanDangKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkCapChinhPhu.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkCapThuTuongChinhPhu.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkCapBoNganh.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkCapTinh.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkCapCoQuanTWDoanThe.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkCapCoSo.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtCoQuanQuanLiTrucTiep.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtBoNganhChuQuan.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkThanhPhan_NhaNuoc.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkThanhPhan_TapThe.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkThanhPhan_TuNhan.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkThanhPhan_VonNuocNgoai.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtTaiSanCoDinh.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtVonLuuDong.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            txtMoTa.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkBaoGom.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkChiBaoGom.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);

            chkLoaiHinh_Truong.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkLoaiHinh_HanhChinh.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkLoaiHinh_DonViNghienCuu.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkLoaiHinh_DonViDichVu.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkLoaiHinh_DoanhNghiepKHCN.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
            chkLoaiHinh_DoanhNghiep.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);


        }

        if (lenh.Equals("backcolor")) // neu la lenh back thi set back cho nó
        {
            // đầu tiên set back các Control phụ

            txtNguoiLapBieu.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtGiamDoc.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtDiaDiemKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkDonViNhan_BoNganhChuQuan.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkDonViNhan_CucThongTin.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkDonViNhan_SoKHCN.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            //
            txtTenDonVi.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtTenGiaoDich.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtTenTiengAnh.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtDiaChi.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtTinhTP.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtDienThoai.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtFax.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtEmail.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtWebsite.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtSoQuyetDinhThanhLap.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtNgayThanhLap.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtMaSoThue.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtSoChungNhanDangKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtNgayChungNhanDangKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtNoiCapChungNhanDangKy.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkCapChinhPhu.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkCapThuTuongChinhPhu.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkCapBoNganh.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkCapTinh.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkCapCoQuanTWDoanThe.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkCapCoSo.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtCoQuanQuanLiTrucTiep.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtBoNganhChuQuan.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkThanhPhan_NhaNuoc.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkThanhPhan_TapThe.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkThanhPhan_TuNhan.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkThanhPhan_VonNuocNgoai.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtTaiSanCoDinh.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtVonLuuDong.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            txtMoTa.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkBaoGom.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkChiBaoGom.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);

            chkLoaiHinh_Truong.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkLoaiHinh_HanhChinh.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkLoaiHinh_DonViNghienCuu.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkLoaiHinh_DonViDichVu.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkLoaiHinh_DoanhNghiepKHCN.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);
            chkLoaiHinh_DoanhNghiep.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);

        }

        // duyet tat các textbox trong bảng phụ
        TextBox tx = null;
        string _tenControl = "", _tenControlTheoDong = "";
        for (int i = 1; i <= _soDong; i++) // vòng lap các dòng
        {
            _tenControlTheoDong = _tenGocControl + i.ToString("D2") + "_"; // lúc này tên control có dạng txt01_
            for (int j = 1; j <= _soCot; j++) // vong lap các cột
            {
                _tenControl = "";// reset lai ten control
                _tenControl = _tenControlTheoDong + j;  // lúc này tên control có dạng txt01_1 
                tx = null; // reset lai bien giữ textbox
                tx = form.FindControl(_tenControl) as TextBox; // tìm textbox với tên đã lấy được
                if (lenh.Equals("khoa"))
                {
                    tx.ReadOnly = true;
                    tx.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
                }
                if (lenh.Equals("backcolor"))
                    tx.BackColor = System.Drawing.Color.FromArgb(255, 255, 204);

            }
        } // het for

    }

    // thuật toán điền dữ liệu lên các dòng - cột
    private void dienDuLieuLenPage(int _soDong, int _soCot, string _tenGocControl, string[] _duLieuNguon, int _dongBatDau)
    {
        TextBox tx = null;
        string _tenControl = "", _tenControlTheoDong = "";
        string[] _arrDuLieuTungDong;
        for (int i = 1; i <= _soDong; i++) // vòng lap các dòng
        {
            _arrDuLieuTungDong = _duLieuNguon[_dongBatDau].Split('-');
            _tenControlTheoDong = _tenGocControl + i.ToString("D2") + "_"; // lúc này tên control có dạng txt01_
            for (int j = 1; j <= _soCot; j++) // vong lap các cột
            {
                _tenControl = "";// reset lai ten control
                _tenControl = _tenControlTheoDong + j;  // lúc này tên control có dạng txt01_1 
                tx = null; // reset lai bien giữ textbox
                tx = form.FindControl(_tenControl) as TextBox; // tìm textbox với tên đã lấy được
                tx.Text = _arrDuLieuTungDong[j - 1];
            }
            _dongBatDau++;
        }
    }


    // thuật toán lấy dữ liệu từ textbox trên page
    private string layDuLieuTrenPage(int _soDong, int _soCot, string _tenGocControl)
    {
        TextBox tx = null;
        string _tenControl = "", _duLieu = "", _tenControlTheoDong = "";
        for (int i = 1; i <= _soDong; i++) // vòng lap các dòng
        {
            _tenControlTheoDong = _tenGocControl + i.ToString("D2") + "_"; // lúc này tên control có dạng txt01_
            for (int j = 1; j <= _soCot; j++) // vong lap các cột
            {
                _tenControl = "";// reset lai ten control
                _tenControl = _tenControlTheoDong + j;  // lúc này tên control có dạng txt01_1 
                tx = null; // reset lai bien giữ textbox
                tx = form.FindControl(_tenControl) as TextBox; // tìm textbox với tên đã lấy được
                _duLieu += tx.Text;
                if ((j + 1) > _soCot)
                    break;
                _duLieu += "-";
            }
            if ((i + 1) > _soDong)
                break;
            _duLieu += "~";
        }
        return _duLieu; // return về 1 chuỗi chứa dữ liệu hàng - cột dạng: " 1-2-3-4-5 , 6-7-8-9-10 " 
    }

    private string layDonViNhan()
    {
        string dvn = "";
        int dem = 0;
        if (chkDonViNhan_CucThongTin.Checked == true)
        {
            dvn += chkDonViNhan_CucThongTin.Text;
            dem++;
        }
        if (chkDonViNhan_SoKHCN.Checked == true)
        {
            dvn += (dem != 0 ? "-" + chkDonViNhan_SoKHCN.Text : chkDonViNhan_SoKHCN.Text);
            dem++;
        }
        if (chkDonViNhan_BoNganhChuQuan.Checked == true)
        {
            dvn += (dem != 0 ? "-" + chkDonViNhan_BoNganhChuQuan.Text : chkDonViNhan_BoNganhChuQuan.Text);
            dem++;
        }

        return dvn;
    }




  
}